const async = require('async');
const usersModel = require('../util/models/users');
const Constant = require('../util/constant');
const Token = require('./token');
const Common = require('../util/common')
const videoModel = require('../util/models/video');
const TOKEN_EXPIRE_SENCOND = 3600;


let exportObj = {
    userLogin,
    userRegister,
    addView
};

module.exports = exportObj;

function userLogin(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        checkParams: (cb) => {
            Common.checkParams(req.body, ['username', 'password'], cb);
        },
        query: ['checkParams', (results, cb) => {
            usersModel
                .findOne({
                    where: {
                        username: req.body.username,
                        password: req.body.password
                    }
                })
                .then(function(result) {
                    if (result) {
                        resObj.data = {
                            id: result.id,
                            username: result.username,
                            nickname: result.nickname,
                            avatar: result.avatar,
                            follows: result.follows,
                            fans: result.fans,
                            video: result.video,
                            introduction: result.introduction,
                        };
                        const adminInfo = {
                            id: result.id
                        };
                        // 生成Token
                        let token = Token.encrypt(adminInfo, TOKEN_EXPIRE_SENCOND);
                        resObj.data.token = token;
                        cb(null, result.id);
                    } else {
                        cb(Constant.LOGIN_ERROR);
                    }
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }],
    };
    Common.autoFn(tasks, res, resObj)
}

function addView(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    var view
    let tasks = {
        checkParams: (cb) => {
            Common.checkParams(req.body, ['id'], cb);
        },
        query: cb => {
            videoModel
                .findOne({ where: { id: req.body.id } })
                .then(function(result) {
                    view = result.views
                    view++
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        },
        update: ['query', (result, cb) => {
            videoModel
                .update({ views: view }, { where: { id: req.body.id } })
                .then(function(result) {
                    resObj.data = view
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }]
    };
    Common.autoFn(tasks, res, resObj)
}

function userRegister(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let username
    let password
    let tasks = {
        checkParams: (cb) => {
            Common.checkParams(req.body, ['username', 'password'], cb);
        },
        query: cb => {
            usersModel
                .findOne({ where: { username: req.body.username } })
                .then(function(result) {
                    console.log(result);
                    if (!result) {
                        username = req.body.username
                        password = req.body.password
                        cb(null)
                    } else {
                        cb(Constant.REGISTER_ERROR)
                    }
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        },
        add: ['query', (result, cb) => {
            console.log(username);
            if (username) {
                usersModel
                    .create({
                        username: username,
                        password: password,
                    })
                    .then(function(result) {
                        resObj.data = result
                        cb(null);
                    })
                    .catch(function(err) {
                        console.log(err);
                        cb(Constant.DEFAULT_ERROR);
                    });
            }
        }]
    };
    Common.autoFn(tasks, res, resObj)

}